﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCDL.BUS;
using TCDL.GLOBAL;
namespace TCDL
{
    public partial class frmChinh_TimKiem : Form
    {
        public static frmChinh_TimKiem f;
        XemDSBUS ds = new XemDSBUS();
        String strMaUser;
        int iCount = 1;
        gConnect[] gCon = new gConnect[1];
        String strLoai;
        DataTable db;
        string strTenKhachHang = "";

        public frmChinh_TimKiem(String strLoai, gConnect[] gCon, String X)
        {
            f = this;
            InitializeComponent();
            db = (DataTable)User.user;
            this.strMaUser = db.Rows[0][0].ToString();
            this.strLoai = strLoai;
            this.gCon = gCon;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void frmChinh_TimKiem_Load(object sender, EventArgs e)
        {
            strTenKhachHang = db.Rows[0][1].ToString() + " " + db.Rows[0][2].ToString();
            if (strLoai.Equals("1"))
            {
                lblWelcome.Text = "Xin chào khách hàng " + strTenKhachHang + "!";
                dgvLichVanChuyen.DataSource = ds.XemDS("sp_XemLenLich", null, 0);
            }
            else
            {
                lblWelcome.Text = "Xin chào nhân viên " + strTenKhachHang + "!";
                gbQLVC.Visible = false;
                btnDangKy.Visible = true;                
                btnThemLenLich.Visible = false;

                int iTest = Int32.Parse(db.Rows[0][7].ToString());
                if (iTest == 1)
                {
                    dgvLichVanChuyen.DataSource = ds.XemDS("sp_XemLenLichTX", null, 0);
                }
                else
                {
                    dgvLichVanChuyen.DataSource = ds.XemDS("sp_XemLenLichPL", null, 0);
                }
            }

            cbxGa.DataSource = ds.XemDS("sp_XemDSGa", null, 0);
            cbxGa.DisplayMember = "TenGa";
            cbxGa.ValueMember = "MaGa";
            cbxTau.DataSource = ds.XemDS("sp_XemDSTau", null, 0);
            cbxTau.DisplayMember = "TenTau";
            cbxTau.ValueMember = "MaTau";

            txtTraTruoc.Text = "";
            txtSoContDK.Text = "";
            txtThanhTien.Text = "";
        }

        private void mnChinh_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void btnDangKy_Click(object sender, EventArgs e)
        {
            string strloaiNV = db.Rows[0][db.Columns.Count - 1].ToString();
            string strMaLL = dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["MaLL"].Value.ToString();
        }

        int SelectedRowIndex = -1;
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvLichVanChuyen.SelectedCells.Count > 0)
            {
                SelectedRowIndex = dgvLichVanChuyen.SelectedCells[0].RowIndex;
            }

            string strMaLL = dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["MaLL"].Value.ToString();
            string strSoContCon = dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["SoContainerCon"].Value.ToString();
            DateTime dtb = DateTime.Now;
            dtb = dtb.AddDays(15);
            string strNgayThanhToan = dtb.ToString();
            txtKhachHang.Text = strTenKhachHang;
            txtVanChuyen.Text = strMaLL;
            txtSoContCon.Text = strSoContCon;
            dtpNgayDen.Text = strNgayThanhToan;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void đãĐăngKýToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (strLoai.Equals("1"))
                new frmChiTietLVC(gCon, strMaUser).Show();
            if (strLoai.Equals("2"))
                new frmChiTietLLV(gCon, strMaUser).Show();
        }

        private void btnDangKy_Click_1(object sender, EventArgs e)
        {
            try
            {
                iCount = 2;
                String strLoai = db.Rows[0][7].ToString();
                String strMaNV = db.Rows[0][0].ToString().Trim();
                int iMaLL = Int32.Parse(txtVanChuyen.Text.Trim());

                if (strLoai.Equals("1"))
                {
                    XemDSBUS ds = new XemDSBUS();
                    gConnect[] gCon = new gConnect[iCount];
                    gCon[0] = new gConnect("@MaNV", strMaNV);
                    gCon[1] = new gConnect("@MaLL", iMaLL);
                    ds.ThemDS("Sp_TaiXeDangKy", gCon, iCount);
                    dgvLichVanChuyen.DataSource = ds.XemDS("sp_XemLenLichTX", null, 0);
                }
                else
                {
                    XemDSBUS ds = new XemDSBUS();
                    gConnect[] gCon = new gConnect[2];
                    gCon[0] = new gConnect("@MaNV", strMaNV);
                    gCon[1] = new gConnect("@MaLL", iMaLL);
                    ds.ThemDS("Sp_PhuLaiDangKy", gCon, iCount);
                    dgvLichVanChuyen.DataSource = ds.XemDS("sp_XemLenLichPL", null, 0);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Có lỗi trong quá trình đăng ký:\n" + ex.Message);
                return;
            }

        }

        private void btnThemLenLich_Click(object sender, EventArgs e)
        {
            try
            {
                iCount = 4;
                gConnect[] gCon = new gConnect[iCount];
                String strMaKH = db.Rows[0][0].ToString();
                
                int iMaLL = Int32.Parse(txtVanChuyen.Text.Trim());
                int iSoContDangKy = Int32.Parse(txtSoContDK.Text.Trim());
                int iTraTruoc = Int32.Parse(txtTraTruoc.Text.Trim());
                int iTien = Int32.Parse(txtThanhTien.Text);

                if (iTraTruoc > iTien / 3)
                {
                    gCon[0] = new gConnect("@MaKH", strMaKH);
                    gCon[1] = new gConnect("@MaLL", iMaLL);
                    gCon[2] = new gConnect("@SoLuongContainer", iSoContDangKy);
                    gCon[3] = new gConnect("@SoTien", iTraTruoc);
                }
                else
                {
                    MessageBox.Show("Tiền trả trước phải cao hơn 1/3 tổng tiền ");
                    return;
                }
                ds.ThemDS("Sp_DangKyVanChuyen", gCon, iCount);
                frmChinh_TimKiem_Load(sender, e);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Có lỗi trong quá trình cập nhật:\n" + ex.Message);
                return;
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            int iMaTau = -1;
            int iMaGa = -1;
            int iSoTien = -1;
            int iContTrong = -1;

            if (chxGa.Checked)
            {
                iMaGa = Int32.Parse(cbxGa.SelectedValue.ToString());
            }
            if (chxTau.Checked)
            {
                iMaTau = Int32.Parse(cbxTau.SelectedValue.ToString());
            }
            if (chxDonGia.Checked && txtDonGia.Text.Trim() != null)
            {
                iSoTien = Int32.Parse(txtDonGia.Text);
            }
            if (chxContTrong.Checked && txtContTrong.Text.Trim() != null)
            {
                iContTrong = Int32.Parse(txtContTrong.Text);
            }

            iCount = 4;
            gConnect[] gCon = new gConnect[iCount];
            gCon[0] = new gConnect("@MaTau", iMaTau);
            gCon[1] = new gConnect("@MaGa", iMaGa);
            gCon[2] = new gConnect("@SoTien", iSoTien);
            gCon[3] = new gConnect("@SoConteainer", iContTrong);
            dgvLichVanChuyen.DataSource = new XemDSBUS().XemDS("sp_TimKiem", gCon, iCount);
        }

        private void txtTraTruoc_MouseClick(object sender, MouseEventArgs e)
        {
            int iCon = Int32.Parse(txtSoContDK.Text);
            int iContTrong = Int32.Parse(txtSoContCon.Text);
            if (iCon < iContTrong)
            {
                string strTongTien = dgvLichVanChuyen.Rows[SelectedRowIndex].Cells["DonGia"].Value.ToString();
                string[] temp = strTongTien.Split('.');
                int iTongTien = Int32.Parse(temp[0]);
                iTongTien = iTongTien * iCon;
                txtThanhTien.Text = iTongTien.ToString();
            }
            else
            {
                MessageBox.Show("Số container đăng ký không đủ");
                return;
            }
        }

        private void txtSoContDK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtTraTruoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoContCon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtContTrong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnThoat_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void thôngTinNhómToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1141308 - Trịnh Bá Ân\n1141373 - Đỗ Nhật Khánh\n1141374 - Nguyễn Thiện Khiêm\n1141409 - Lê Thành Nhân");
        }
    }
}
